package com.pingtai.indent.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.pingtai.common.entity.IndexPointOut;
import com.pingtai.filter.ChildUserFilter;
import com.pingtai.indent.entity.IndentQqPlzb;
import com.pingtai.indent.entity.IndentXinlangZb;
import com.pingtai.indent.entity.IndentXinlangZbType;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.entity.XinlangPersonRenzheng;
import com.pingtai.indent.entity.XinlangPrimaryFanStatistics;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.manage.service.ProduceManageService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class XinlangIndentAction {
	
	private UserBaseIndentType baseIndentType;
	private WeiboIndentService weiboIndentService;
	private String base;
	private UserManagerService userManagerService;
	private User curUser; 
	private Integer currentPage=1;
	private Integer pageNum;
	private PaginationSupport paginationSupport;
	private String current;
	private UserIndentSearch qe;
	private String queryString;
	private Float price;
	private UserIndent userIndent;
	private List indentXinlangZbTypeList;
private String typeRadio;
	
	public String getTypeRadio() {
		return typeRadio;
	}
	public void setTypeRadio(String typeRadio) {
		this.typeRadio = typeRadio;
	}
	
	
	public List getIndentXinlangZbTypeList() {
		return indentXinlangZbTypeList;
	}

	public void setIndentXinlangZbTypeList(List indentXinlangZbTypeList) {
		this.indentXinlangZbTypeList = indentXinlangZbTypeList;
	}

	public UserIndent getUserIndent() {
		return userIndent;
	}

	public void setUserIndent(UserIndent userIndent) {
		this.userIndent = userIndent;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}

	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}

	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}

	public UserIndentSearch getQe() {
		return qe;
	}

	public void setQe(UserIndentSearch qe) {
		this.qe = qe;
	}
	
	public String xinlangPersonRenzheng2IndentManageUI() {
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		curUser = userManagerService.findCurrectUser();
		if (currentPage > 0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE * (currentPage - 1);
		}

		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while (childIt.hasNext()) {
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while (chIt.hasNext()) {
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		if (userManagerService.isNotUser(curUser)
				&& !userManagerService.isUser_2(curUser)) {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, true, baseList);
		} else {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, false, baseList);
		}
		if (qe != null) {
			current = qe.getStatus();
		} else {
			current = "all";
		}

		return "success";
	}
	
	public String xinlangPersonRenzheng2ManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while(chIt.hasNext()){
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList,qe,"xinlangPersonRenzheng");
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList,qe,"xinlangPersonRenzheng");
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	private ProduceManageService produceManageService;
	private IndexPointOut indexPointOut;
	
	
	
	public ProduceManageService getProduceManageService() {
		return produceManageService;
	}

	public void setProduceManageService(ProduceManageService produceManageService) {
		this.produceManageService = produceManageService;
	}

	public IndexPointOut getIndexPointOut() {
		return indexPointOut;
	}

	public void setIndexPointOut(IndexPointOut indexPointOut) {
		this.indexPointOut = indexPointOut;
	}

	public String toXinlangPersonRenzheng2Add(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		indexPointOut = produceManageService.getIndexPointOut(2);
		baseIndentTypeList = new ArrayList();
		priceList = new ArrayList();
		
		baseIndentTypeMinAmountList= new ArrayList();
		baseIndentTypeMaxAmountList= new ArrayList();
		baseIndentTypeUnitPriceList= new ArrayList();
		baseIndentTypeNameList= new ArrayList();
		baseIndentTypeUnitNameList= new ArrayList();
		
		indentTypeList = new ArrayList();
		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			Set s = baseIndentType.getChildrenBaseIndentType();
			Iterator it = s.iterator();
			while(it.hasNext()){
				UserBaseIndentType b = (UserBaseIndentType) it.next();
				if(b.getEnable()!=1) continue;
				baseIndentTypeList.add(b);
			}
			
			if(baseIndentTypeList!=null&&baseIndentTypeList.size()>0){
				for(int i=0;i<baseIndentTypeList.size();i++){
					UserBaseIndentType b = (UserBaseIndentType) baseIndentTypeList.get(i);
					Float p = b.getPrice();
					priceList.add(p);
				}
			}else{
				price = baseIndentType.getPrice();
			}
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				Set s2 = indentType.getChildrenIndentType();
				Iterator it2 = s2.iterator();
				while(it2.hasNext()){
					UserIndentType idt = (UserIndentType) it2.next();
					if(idt.getUserBaseIndentType().getEnable()!=1) continue;
					indentTypeList.add(idt);
				}
				
				if(indentTypeList!=null&&indentTypeList.size()>0){
					indentTypeList = Utils.menuSort(indentTypeList);
					for(int i=0;i<indentTypeList.size();i++){
						UserIndentType idt = (UserIndentType) indentTypeList.get(i);
						UserBaseIndentType bdt = idt.getUserBaseIndentType();
						baseIndentTypeList.add(bdt);
						idt = weiboIndentService.updateIndentType(idt);
						Float p = idt.getPrice();
						priceList.add(p);
						baseIndentTypeMinAmountList.add(bdt.getMinAmount());
						baseIndentTypeMaxAmountList.add(bdt.getMaxAmount());
						baseIndentTypeUnitPriceList.add(bdt.getUnit2());
						baseIndentTypeNameList.add(bdt.getName());
						baseIndentTypeUnitNameList.add(bdt.getUnit());
					}
				}else{
					price = indentType.getPrice();
				}
			}
		}
		return "success";
	}
	
	public String xinlangPersonRenzheng2Add(){
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));
		curUser = userManagerService.findCurrectUser();
		//判断价钱是否合法
			boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
			if(flag){
				
				userIndent.setCreateDate(new Date());
				User parentUser = userManagerService.findUserById(curUser.getParentUserId());
				userIndent.setParentUser(parentUser);
				userIndent.setState(0);
				
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
				
				userIndent.setType(new Integer(base));
				userIndent.setUser(curUser);
				Float t = price*1/baseIndentType.getUnit2();
				userIndent.setTotalPrice(t);
				userIndent.setPrice(price);
				//判余额是否充足
				Float money = curUser.getMoney();
				Float money2 = (float) Math.round(money*100/100);
				if(money2<t){
					return "moneyLow";
				}
				userIndent.setBasePrice(baseIndentType.getPrice());
				Float baseTotalPrice = baseIndentType.getPrice()*1/baseIndentType.getUnit2();
				userIndent.setBaseTotalPrice(baseTotalPrice);
				
				userIndent.setBaseIndentType(baseIndentType);
				
				XinlangPersonRenzheng xinlangPersonRenzheng = userIndent.getRenzheng().getXinlangPersonRenzheng();
				xinlangPersonRenzheng.setState(userIndent.getState());
				
				String remark = xinlangPersonRenzheng.getRemark();
				remark = Utils.replaceEnter(remark);
				xinlangPersonRenzheng.setRemark(remark);
				
				weiboIndentService.saveXinlangPersionRenzheng(xinlangPersonRenzheng,
						userIndent,baseIndentType,curUser,indentType,t,price);
				
			}else{
				return "add_indent_403";
			}
			return "success";
	}
	
	public String toXinlangRenzheng2IndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String toXinlangRenzheng2IndentDetail(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	public String toXinlangRenzheng2IndentModify(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	public String xinlangRenzheng2IndentModify(){
		Integer state = userIndent.getState();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(state);
		userIndent.setState(state);
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(state);
		weiboIndentService.saveUserIndent(userIndent);
		baseIndentType = userIndent.getBaseIndentType();
		return "success";
	}
	
	public String toXinlangRenzheng2IndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String xinlangRenzheng2IndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(6);
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(6);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.saveUserIndent(userIndent);
		baseIndentType = userIndent.getBaseIndentType();
		return "success";
	}

	public String xinlangPersonRenzhengIndentManageUI(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while(chIt.hasNext()){
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
	    
		return "success";
	}
	
	public String xinlangPersonRenzhengManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while(chIt.hasNext()){
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList,qe,"xinlangPersonRenzheng");
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList,qe,"xinlangPersonRenzheng");
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	private List baseIndentTypeList;
	private List priceList;
	private List baseIndentTypeMinAmountList;
	private List baseIndentTypeUnitPriceList;
	private List baseIndentTypeMaxAmountList;
	private List baseIndentTypeNameList;
	private List baseIndentTypeUnitNameList;
	private List indentTypeList;
	
	
	public List getIndentTypeList() {
		return indentTypeList;
	}

	public void setIndentTypeList(List indentTypeList) {
		this.indentTypeList = indentTypeList;
	}

	public List getBaseIndentTypeList() {
		return baseIndentTypeList;
	}

	public void setBaseIndentTypeList(List baseIndentTypeList) {
		this.baseIndentTypeList = baseIndentTypeList;
	}

	public List getPriceList() {
		return priceList;
	}

	public void setPriceList(List priceList) {
		this.priceList = priceList;
	}

	public List getBaseIndentTypeMinAmountList() {
		return baseIndentTypeMinAmountList;
	}

	public void setBaseIndentTypeMinAmountList(List baseIndentTypeMinAmountList) {
		this.baseIndentTypeMinAmountList = baseIndentTypeMinAmountList;
	}

	public List getBaseIndentTypeUnitPriceList() {
		return baseIndentTypeUnitPriceList;
	}

	public void setBaseIndentTypeUnitPriceList(List baseIndentTypeUnitPriceList) {
		this.baseIndentTypeUnitPriceList = baseIndentTypeUnitPriceList;
	}

	public List getBaseIndentTypeMaxAmountList() {
		return baseIndentTypeMaxAmountList;
	}

	public void setBaseIndentTypeMaxAmountList(List baseIndentTypeMaxAmountList) {
		this.baseIndentTypeMaxAmountList = baseIndentTypeMaxAmountList;
	}

	public List getBaseIndentTypeNameList() {
		return baseIndentTypeNameList;
	}

	public void setBaseIndentTypeNameList(List baseIndentTypeNameList) {
		this.baseIndentTypeNameList = baseIndentTypeNameList;
	}

	public List getBaseIndentTypeUnitNameList() {
		return baseIndentTypeUnitNameList;
	}

	public void setBaseIndentTypeUnitNameList(List baseIndentTypeUnitNameList) {
		this.baseIndentTypeUnitNameList = baseIndentTypeUnitNameList;
	}

	public String toXinlangPersonRenzhengAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		baseIndentTypeList = new ArrayList();
		priceList = new ArrayList();
		
		baseIndentTypeMinAmountList= new ArrayList();
		baseIndentTypeMaxAmountList= new ArrayList();
		baseIndentTypeUnitPriceList= new ArrayList();
		baseIndentTypeNameList= new ArrayList();
		baseIndentTypeUnitNameList= new ArrayList();
		
		indentTypeList = new ArrayList();
		if (baseIndentType.getEnable() == 0) {
			return "produceWeihu";
		}

		curUser = userManagerService.findCurrectUser();
		if (userManagerService.isRoot(curUser)) {
			Set s = baseIndentType.getChildrenBaseIndentType();
			Iterator it = s.iterator();
			while(it.hasNext()){
				UserBaseIndentType b = (UserBaseIndentType) it.next();
				if(b.getEnable()!=1) continue;
				baseIndentTypeList.add(b);
			}
			
			if(baseIndentTypeList!=null&&baseIndentTypeList.size()>0){
				for(int i=0;i<baseIndentTypeList.size();i++){
					UserBaseIndentType b = (UserBaseIndentType) baseIndentTypeList.get(i);
					Float p = b.getPrice();
					priceList.add(p);
				}
			}else{
				price = baseIndentType.getPrice();
			}
		} else {
			UserIndentType indentType = weiboIndentService
					.getUserIndentTypeByUserAndType(curUser,
							baseIndentType.getId());
			if (indentType == null) {
				return "403";
			} else {
				Set s2 = indentType.getChildrenIndentType();
				Iterator it2 = s2.iterator();
				while(it2.hasNext()){
					UserIndentType idt = (UserIndentType) it2.next();
					if(idt.getUserBaseIndentType().getEnable()!=1) continue;
					indentTypeList.add(idt);
				}
				
				if(indentTypeList!=null&&indentTypeList.size()>0){
					indentTypeList = Utils.menuSort(indentTypeList);
					for(int i=0;i<indentTypeList.size();i++){
						UserIndentType idt = (UserIndentType) indentTypeList.get(i);
						UserBaseIndentType bdt = idt.getUserBaseIndentType();
						baseIndentTypeList.add(bdt);
						idt = weiboIndentService.updateIndentType(idt);
						Float p = idt.getPrice();
						priceList.add(p);
						baseIndentTypeMinAmountList.add(bdt.getMinAmount());
						baseIndentTypeMaxAmountList.add(bdt.getMaxAmount());
						baseIndentTypeUnitPriceList.add(bdt.getUnit2());
						baseIndentTypeNameList.add(bdt.getName());
						baseIndentTypeUnitNameList.add(bdt.getUnit());
					}
				}else{
					price = indentType.getPrice();
				}
			}
		}
		return "success";
	}
	
	public String xinlangPersonRenzhengAdd(){
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));
		curUser = userManagerService.findCurrectUser();
		//判断价钱是否合法
			boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
			if(flag){
				
				userIndent.setCreateDate(new Date());
				User parentUser = userManagerService.findUserById(curUser.getParentUserId());
				userIndent.setParentUser(parentUser);
				userIndent.setState(0);
				
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
				
				userIndent.setType(new Integer(base));
				userIndent.setUser(curUser);
				Float t = price*1/baseIndentType.getUnit2();
				userIndent.setTotalPrice(t);
				userIndent.setPrice(price);
				//判余额是否充足
				Float money = curUser.getMoney();
				Float money2 = (float) Math.round(money*100/100);
				if(money2<t){
					return "moneyLow";
				}
				userIndent.setBasePrice(baseIndentType.getPrice());
				Float baseTotalPrice = baseIndentType.getPrice()*1/baseIndentType.getUnit2();
				userIndent.setBaseTotalPrice(baseTotalPrice);
				
				userIndent.setBaseIndentType(baseIndentType);
				
				XinlangPersonRenzheng xinlangPersonRenzheng = userIndent.getRenzheng().getXinlangPersonRenzheng();
				xinlangPersonRenzheng.setState(userIndent.getState());
				
				String remark = xinlangPersonRenzheng.getRemark();
				remark = Utils.replaceEnter(remark);
				xinlangPersonRenzheng.setRemark(remark);
				
				weiboIndentService.saveXinlangPersionRenzheng(xinlangPersonRenzheng,
						userIndent,baseIndentType,curUser,indentType,t,price);
				
			}else{
				return "add_indent_403";
			}
			return "success";
	}
	
	public String toXinlangRenzhengIndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String toXinlangRenzhengIndentDetail(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	public String toXinlangRenzhengIndentModify(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	public String xinlangRenzhengIndentModify(){
		Integer state = userIndent.getState();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(state);
		userIndent.setState(state);
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(state);
		weiboIndentService.saveUserIndent(userIndent);
		baseIndentType = userIndent.getBaseIndentType();
		return "success";
	}
	
	public String toXinlangRenzhengIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String xinlangRenzhengIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(6);
		userIndent.getRenzheng().getXinlangPersonRenzheng().setState(6);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.saveUserIndent(userIndent);
		baseIndentType = userIndent.getBaseIndentType();
		return "success";
	}
	
	public String xinlangZbIndentManageUI(){
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));
		
		curUser = userManagerService.findCurrectUser();
		if (currentPage > 0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE * (currentPage - 1);
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
		}
		
		if (userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)) {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, true, baseList);
		} else {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, false, baseList);
		}
		if (qe != null) {
			current = qe.getStatus();
		} else {
			current = "all";
		}

		return "success";
	}
	
	public String xinlangZbIndentManageUIForSearch(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList,qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList,qe);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toXinlangWeiboAddForZb(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		baseIndentTypeList = new ArrayList();
		priceList = new ArrayList();
		
		baseIndentTypeMinAmountList= new ArrayList();
		baseIndentTypeMaxAmountList= new ArrayList();
		baseIndentTypeUnitPriceList= new ArrayList();
		baseIndentTypeNameList= new ArrayList();
		baseIndentTypeUnitNameList= new ArrayList();
		
		
		indentTypeList = new ArrayList();
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		
		
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			
			Set s = baseIndentType.getChildrenBaseIndentType();
			Iterator it = s.iterator();
			while(it.hasNext()){
				UserBaseIndentType b = (UserBaseIndentType) it.next();
				if(b.getEnable()!=1) continue;
				baseIndentTypeList.add(b);
			}
			
			if(baseIndentTypeList!=null&&baseIndentTypeList.size()>0){
				for(int i=0;i<baseIndentTypeList.size();i++){
					UserBaseIndentType b = (UserBaseIndentType) baseIndentTypeList.get(i);
					Float p = b.getPrice();
					priceList.add(p);
				}
			}else{
				price = baseIndentType.getPrice();
			}
			
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				
				Set s2 = indentType.getChildrenIndentType();
				Iterator it2 = s2.iterator();
				while(it2.hasNext()){
					UserIndentType idt = (UserIndentType) it2.next();
					if(idt.getUserBaseIndentType().getEnable()!=1) continue;
					indentTypeList.add(idt);
				}
				
				if(indentTypeList!=null&&indentTypeList.size()>0){
					indentTypeList = Utils.menuSort(indentTypeList);
					for(int i=0;i<indentTypeList.size();i++){
						UserIndentType idt = (UserIndentType) indentTypeList.get(i);
						UserBaseIndentType bdt = idt.getUserBaseIndentType();
						baseIndentTypeList.add(bdt);
						idt = weiboIndentService.updateIndentType(idt);
						Float p = idt.getPrice();
						priceList.add(p);
						baseIndentTypeMinAmountList.add(bdt.getMinAmount());
						baseIndentTypeMaxAmountList.add(bdt.getMaxAmount());
						baseIndentTypeUnitPriceList.add(bdt.getUnit2());
						baseIndentTypeNameList.add(bdt.getName());
						baseIndentTypeUnitNameList.add(bdt.getUnit());
					}
				}else{
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
				}
				
				
			}
		}
		indentXinlangZbTypeList = weiboIndentService.getXinlangZbTypeList();
		return "success";
	}
	
	private String descp;
	
	
	

	public String getDescp() {
		return descp;
	}

	public void setDescp(String descp) {
		this.descp = descp;
	}

	private IndentXinlangZbType indentXinlangZbType;
	
	
	public IndentXinlangZbType getIndentXinlangZbType() {
		return indentXinlangZbType;
	}

	public void setIndentXinlangZbType(IndentXinlangZbType indentXinlangZbType) {
		this.indentXinlangZbType = indentXinlangZbType;
	}


	public String addXinlangZbIndent(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			userIndent.setCreateDate(new Date());
			
			userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			indentXinlangZbType = weiboIndentService.getXinlangZbTypeById(indentXinlangZbType.getId());
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			IndentXinlangZb indentXinlangZb = new IndentXinlangZb();
			descp = Utils.replaceEnter(descp);
			indentXinlangZb.setDescp(descp);
			indentXinlangZb.setIndentXinlangZbType(indentXinlangZbType);
			indentXinlangZb = weiboIndentService.saveIndentXinlangZb(indentXinlangZb);
			
			userIndent.setIndentXinlangZb(indentXinlangZb);
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			userIndent.setBaseIndentType(baseIndentType);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	private List indentNumberTmpList;
	
	public List getIndentNumberTmpList() {
		return indentNumberTmpList;
	}

	public void setIndentNumberTmpList(List indentNumberTmpList) {
		this.indentNumberTmpList = indentNumberTmpList;
	}

	public String toIndentDetails_xl_zb(){
    	userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		return "success";
    }
	
	
	public String addXinlangPrimaryFanIndent(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			
			
			userIndent.setCreateDate(new Date());
			
			userIndent.setNowNumber(userIndent.getStartNumber());
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			userIndent.setParentUser(parentUser);
			userIndent.setState(0);
			//indentXinlangZbType = weiboIndentService.getXinlangZbTypeById(indentXinlangZbType.getId());
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			if(userManagerService.isRoot(curUser)){
				price = baseIndentType.getPrice();
			}else{
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				if(indentType==null){
					return "403";
				}else{
					Float oldPrice = indentType.getPrice();
					indentType = weiboIndentService.updateIndentType(indentType);
					price = indentType.getPrice();
					if(!oldPrice.equals(price)){
						return "priceChange";
					}
				}
			}
			
			userIndent.setType(new Integer(base));
			userIndent.setUser(curUser);
			Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
			userIndent.setTotalPrice(t);
			userIndent.setPrice(price);
			
			//判余额是否充足
			Float money = curUser.getMoney();
			Float money2 = (float) Math.round(money*100/100);
			if(money2<t){
				return "moneyLow";
			}
			userIndent.setBasePrice(baseIndentType.getPrice());
			Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
			userIndent.setBaseTotalPrice(baseTotalPrice);
			userIndent.setBaseIndentType(baseIndentType);
			//保存订单表
			userIndent = weiboIndentService.saveUserIndent(userIndent);
			
			//所需金额保存到临时表
			UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
			userIndentMoneyTmp.setMoney(t);
			userIndentMoneyTmp.setUser(curUser);
			userIndentMoneyTmp.setUserIndent(userIndent);
			userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
			//从用户账户中的总金额减去订单总价 保存
			money = curUser.getMoney();
			money = money -t;
			curUser.setMoney(money);
			UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
			
			weiboIndentService.getIndentDao().evict(userIndent);
			weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
			weiboIndentService.getIndentDao().evict(userPay);
			
			
			//保存返利关系
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
			//userIndent2 = userIndent;
			userIndent=null;
			
		}else{
			return "add_indent_403";
		}
	
	return "success";
	}
	
	private XinlangPrimaryFanStatistics xlStatistics;
	
	
	
	public XinlangPrimaryFanStatistics getXlStatistics() {
		return xlStatistics;
	}

	public void setXlStatistics(XinlangPrimaryFanStatistics xlStatistics) {
		this.xlStatistics = xlStatistics;
	}

	public String toXinlangPrimaryFanCompleteStatistics(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		xlStatistics = new XinlangPrimaryFanStatistics();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String d = format.format(now);
        xlStatistics.setCompleteDate(d);
        
		List list = weiboIndentService.getAllIndentOfCompletedByDate(xlStatistics.getCompleteDate(),baseIndentType);
		xlStatistics.setList(list);
		xlStatistics.setNumber(list.size());
		Integer n = 0;
		for(int i=0;i<list.size();i++){
			UserIndent indent = (UserIndent) list.get(i);
			n+=indent.getNumber();
		}
		xlStatistics.setTotalFanNum(n);
		return "success";
	}
	
	public String xinlangPrimaryFanCompleteStatistics(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		if(xlStatistics==null){
			return "success";
		}
		List list = weiboIndentService.getAllIndentOfCompletedByDate(xlStatistics.getCompleteDate(),baseIndentType);
		xlStatistics.setList(list);
		xlStatistics.setNumber(list.size());
		Integer n = 0;
		for(int i=0;i<list.size();i++){
			UserIndent indent = (UserIndent) list.get(i);
			n+=indent.getNumber();
		}
		xlStatistics.setTotalFanNum(n);
		return "success";
	}
	
	public String toXinlangPrimaryFanIncrement(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		xlStatistics = new XinlangPrimaryFanStatistics();
		return "success";
	}
	
	public String xinlangPrimaryFanIncrement() throws ParseException{
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		if(xlStatistics==null){
			return "success";
		}
		
		List list = weiboIndentService.getAllIndentNumIncrementList(xlStatistics.getCompleteDate(),baseIndentType);
		xlStatistics.setList(list);
		Integer n = 0;
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				XinlangPrimaryFanStatistics s = (XinlangPrimaryFanStatistics) list.get(i);
				n += s.getTotalFanNum();
			}
		}
		xlStatistics.setTotalFanNum(n);
		xlStatistics.setNumber(list.size());
		
		return "success";
	}
}
